package com.abc.easyclaim;

import java.util.ArrayList;
import java.util.List;

import com.abc.bean.Record;
import com.abc.db.DBManager;
import com.abc.tool.StringTool;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

//更新界面显示的Intent接收者
public class UIUpdateReceiver extends BroadcastReceiver {
    int status=Constant.STATUS_STOP;//初始状态为停止状态
    int currentSound=-1;                  //当前播放录音序号
    int progress;          //进度条值
    private DBManager dbManager;           //数据库操作管理器
	private List<Record> records=new ArrayList<Record>();         //录音集合
    Activity ac;//此Receiver对应的Activity
    public UIUpdateReceiver(Activity ac)
    {
    	this.ac=ac; 
    }
    
	@Override
	public void onReceive(Context context, Intent intent) {
		//从接收的Intent中获取状态值
		status=intent.getIntExtra("status", -1);
		//从接收的Intent中获取当前播放录音序号
		currentSound=intent.getIntExtra("currentSound", -1);
		//接收播放进度条值
		progress=intent.getIntExtra("progress", 0);
		System.out.println("uiu.progress="+progress);
		//从数据库中读取录音信息
		dbManager=new DBManager(context);
		records=dbManager.queryRecords(MainApplication.getCurrentCaseId());
		
		//获取播放/暂停按钮的引用
		ImageButton playbtn=(ImageButton)ac.findViewById(R.id.play_sound);
		//获取停止播放按钮的引用
		ImageButton stopbtn=(ImageButton)ac.findViewById(R.id.play_stopsound);
		TextView playNametv=(TextView)ac.findViewById(R.id.play_soundname);            //播放文件名称
		TextView playSoundDatetv=(TextView)ac.findViewById(R.id.play_soundtime);      //播放文件的录制日期
		final TextView playTimetv=(TextView)ac.findViewById(R.id.play_time);                 //已播放时间长度
		TextView playLongtv=(TextView)ac.findViewById(R.id.play_long);                  //文件播放长度
		TextView playSoundDescriptiontv=(TextView)ac.findViewById(R.id.play_sounddescription);//播放文件的描述信息
		//获取播放进度条的引用
		final SeekBar seekBar=(SeekBar)ac.findViewById(R.id.play_seekBar);
		switch(status)
		{
		  case Constant.STATUS_PLAY://若更新到播放状态则将图片换为按下后暂停的提示图片			 
			  playbtn.setImageResource(R.drawable.play_go);	
			  //接收到currrentSound则说明是新的一轮播放
			  if (currentSound!=-1) {
				 // stopbtn.setImageResource(R.drawable.stop);
				  stopbtn.setClickable(true);              //设为可以点击
				  playNametv.setText(records.get(currentSound).getTitle());
				  playSoundDatetv.setText("录制于 "+StringTool.date2String(records.get(currentSound).getDate(), "yyyy-MM-dd HH:mm:ss"));
				  playTimetv.setText("00:00:00");
				  playLongtv.setText(getPlayTime(records.get(currentSound).getLength()));
				  seekBar.setMax(records.get(currentSound).getLength());   //设置进度条最大值
				  playSoundDescriptiontv.setText(records.get(currentSound).getDescription());
			  }
		      break;
		  case Constant.STATUS_STOP:  //若更新到停止、暂停状态则将图片换为按下后播放的提示图片	
			  playbtn.setImageResource(R.drawable.play_go);
			  //stopbtn.setImageResource(R.drawable.grey_stop);
			  stopbtn.setClickable(false);            //设为不可点击
			  if (currentSound!=-1) {              //说明是播放下一个或者播放上一个
				  playNametv.setText(records.get(currentSound).getTitle());
				  playSoundDatetv.setText("录制于 "+StringTool.date2String(records.get(currentSound).getDate(), "yyyy-MM-dd HH:mm:ss"));
				  playTimetv.setText("00:00:00");
				  playLongtv.setText(getPlayTime(records.get(currentSound).getLength()));
				  seekBar.setMax(records.get(currentSound).getLength());   //设置进度条最大值
				  playSoundDescriptiontv.setText(records.get(currentSound).getDescription());
			  }
			  break;
		  case Constant.STATUS_PAUSE:
			  playbtn.setImageResource(R.drawable.play_go);		
		      break;
		}
		System.out.println("this.progress="+progress);
		seekBar.setProgress(progress);  //设置进度条值
		playTimetv.setText(getPlayTime(progress));    //显示已播放时间
	}
	
	//获取播放总长度字符函数
    public static String getPlayTime(int playlong) {
		String result=null; 
    	//音频总长度
    	int longhour=playlong/3600;
    	int longminutes=(playlong-longhour*3600)/60;
    	int longseconds=playlong-longhour*3600-longminutes*60;
    	
    	result=longhour+":"+((longminutes<10)?("0"+longminutes):longminutes)+":"+
    			((longseconds<10)?("0"+longseconds):longseconds);
		return result;
	}
}
